﻿Public Class EventInfo
    Public strName As String
    Public iLine As Integer

    Public Sub New(ByVal Name As String, ByVal Line As Integer)
        strName = Name
        iLine = Line
    End Sub
End Class

Public Class TodoInfo
    Public strInfo As String
    Public iLine As Integer

    Public Sub New(ByVal Info As String, ByVal Line As Integer)
        strInfo = Info
        iLine = Line
    End Sub
End Class

Public Class Project
    Public strName As String
    Public strFilepath As String
    Public EventList As ArrayList
    Public TodoList As ArrayList

    Public Sub New(ByVal Name As String, ByVal Filepath As String)
        strName = Name
        strFilepath = Filepath

        EventList = New ArrayList
        TodoList = New ArrayList
    End Sub

    Public Sub Save(ByVal strFilepath As String)
        Try
            If (strFilepath = "" Or strName = "") Then Return
            If (frmMain.OpenProjects.ContainsKey(strName) = False) Then Return

            Dim i As Integer = frmMain.tcMain.TabPages.IndexOfKey("tp" + strName)

            If (i = -1) Then Return

            Dim tpTemp As TabPage = frmMain.tcMain.TabPages.Item(i)
            Dim txtEditArea As RichTextBox = tpTemp.Controls("txtEditArea")

            txtEditArea.SaveFile(strFilepath)

        Catch ex As Exception
            frmMain.HandleException(ex)
        End Try
    End Sub

    Public Sub UpdateEvents(Optional ByVal bUpdateHighlighting As Boolean = False)
        Try
            If (frmMain.OpenProjects.Contains(strName) = False) Then Return
            If (frmMain.tcMain.TabPages.Count() = 0) Then Return
            If (frmMain.tcMain.TabPages.ContainsKey("tp" + strName) = False) Then Return

            Dim txtEditArea As RichTextBox = frmMain.tcMain.TabPages("tp" + strName).Controls("txtEditArea")

            EventList.Clear()
            TodoList.Clear()

            Dim i As Integer = 0
            Dim c As Integer = 0

            Dim iSelection = txtEditArea.SelectionStart
            Dim iLength = txtEditArea.SelectionLength

            If (bUpdateHighlighting = True) Then txtEditArea.Hide()

            Dim tLine As String = ""
            Dim tCommand As String = ""
            Dim iCommandEnd As Integer = 0

            For Each s As String In txtEditArea.Lines
                i += 1

                If (bUpdateHighlighting = True) Then
                    tLine = frmMain.CleanSTR(s)
                    tCommand = tLine.Split().ElementAt(0)

                    If (tLine.StartsWith("Event") = True) Then
                        For Each e As String In EventNames
                            If (tLine.Contains(e) = True) Then
                                frmMain.HighlightChecker.ApplyToWord(e, txtEditArea, c + (s.IndexOf(e)), e.Length)
                            End If
                        Next
                    End If

                    If (tLine.StartsWith("@") = True) Then
                        tCommand = tLine.Split.ElementAt(0)

                        frmMain.HighlightChecker.ApplyToWord(tCommand, txtEditArea, c + (s.Length - tLine.Length), tCommand.Length)
                    End If

                    If (Keywords.Contains(tCommand)) Then
                        frmMain.HighlightChecker.ApplyToWord(tCommand, txtEditArea, c + (s.Length - tLine.Length), tCommand.Length)
                    End If

                    If (tLine.StartsWith("/") = True) Then
                        tCommand = tLine.Split().ElementAt(0)
                        frmMain.HighlightChecker.ApplyToWord(tCommand, txtEditArea, c + (s.Length - tLine.Length), tCommand.Length)
                    End If
                End If

                If (s.Split.Contains("Event") = True) Then
                    Dim tInfo As New EventInfo("", i)

                    For Each t As String In s.Split()
                        If (t <> "Event" And t.Length > 0) Then
                            tInfo.strName = t
                            EventList.Add(tInfo)

                            Exit For
                        End If
                    Next

                ElseIf (s.Split.Contains("@todo") = True) Then
                    tLine = frmMain.CleanSTR(s)

                    If (tLine.StartsWith("@todo") = True) Then
                        TodoList.Add(New TodoInfo(frmMain.CleanSTR(tLine.Substring(5)), i))
                    End If
                End If

                c += s.Length() + 1
            Next

            If bUpdateHighlighting = True Then
                txtEditArea.SelectionStart = 0
                txtEditArea.SelectionLength = 0
            End If

            If (bUpdateHighlighting = True) Then txtEditArea.Show()

        Catch ex As Exception
            frmMain.HandleException(ex)
        End Try
    End Sub
End Class
